package com.bingfor.easycablelink.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bingfor.easycablelink.R;
import com.bingfor.easycablelink.base.BaseFragment;
import com.bingfor.easycablelink.base.ViewPagerFragmentAdapter;
import com.bingfor.easycablelink.databinding.PageHomeBinding;
import com.bingfor.easycablelink.home.Fragment.Cable.CableFragment;
import com.bingfor.easycablelink.home.Fragment.Dynamic.DynamicFragment;
import com.bingfor.easycablelink.home.Fragment.logistical.LogisticalFragment;
import com.bingfor.easycablelink.home.Fragment.Support.SupportingFragment;
import com.bingfor.easycablelink.home.Fragment.lore.loreFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LY on 2017/5/17.
 */

public class HomePage extends BaseFragment{

    PageHomeBinding binding ;
    TabLayout mTabLayout;
    List<String> titleList = new ArrayList<>();
    List<BaseFragment> fragmentList = new ArrayList<>();
    ViewPagerFragmentAdapter adapter ;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = PageHomeBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState ){
        super.onActivityCreated(savedInstanceState);
        initViews();
    }
    @Override
    public void refresh() {

    }

    @Override
    public void initViews() {
        mTabLayout = $(R.id.mTabLayout);
        ViewPager mViewPager = $(R.id.mViewPager);
        adapter = new ViewPagerFragmentAdapter(mContext.getSupportFragmentManager(), fragmentList, titleList);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(10);
        mTabLayout.setupWithViewPager(mViewPager);
        initData();
    }

    private void initData(){
        titleList.add("线缆");
        titleList.add("配套");
        titleList.add("物流");
        titleList.add("知识");
        titleList.add("动态");

        fragmentList.add( CableFragment.getInstance(0)) ;
        fragmentList.add( LogisticalFragment.getInstance(1));
        fragmentList.add( SupportingFragment.getInstance(2));
        fragmentList.add( loreFragment.getInstance(3));
        fragmentList.add( DynamicFragment.getInstance(4));

        for (int i = 0; i <5; i++) {

            Bundle bundle = new Bundle();
            bundle.putSerializable("item",titleList.get(i));
            bundle.putInt("id",i);
            fragmentList.get(i).setArguments(bundle);
            titleList.add(i+"");
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void initToolbar() {

    }

    public static HomePage getInstance() {
        return new HomePage();
    }
}
